/**
 * 消息工具类
 * @author Wu.Liang
 * @date 2024-12-21
 * @description 统一处理Element Plus消息提示
 */
import { ElMessage, type MessageParamsWithType } from 'element-plus'

/**
 * 消息类型
 */
export type MessageType = 'success' | 'warning' | 'info' | 'error'

/**
 * 消息配置接口
 */
export interface MessageConfig {
  type: MessageType
  message: string
  duration?: number
  showClose?: boolean
}

/**
 * 显示成功消息
 * @param message 消息内容
 * @param duration 显示时长（毫秒）
 */
export const showSuccess = (message: string, duration: number = 3000) => {
  ElMessage({
    type: 'success',
    message,
    duration
  } as unknown as MessageParamsWithType)
}

/**
 * 显示警告消息
 * @param message 消息内容
 * @param duration 显示时长（毫秒）
 */
export const showWarning = (message: string, duration: number = 4000) => {
  ElMessage({
    type: 'warning',
    message,
    duration
  } as unknown as MessageParamsWithType)
}

/**
 * 显示信息消息
 * @param message 消息内容
 * @param duration 显示时长（毫秒）
 */
export const showInfo = (message: string, duration: number = 3000) => {
  ElMessage({
    type: 'info',
    message,
    duration
  } as unknown as MessageParamsWithType)
}

/**
 * 显示错误消息
 * @param message 消息内容
 * @param duration 显示时长（毫秒）
 */
export const showError = (message: string, duration: number = 5000) => {
  ElMessage({
    type: 'error',
    message,
    duration
  } as unknown as MessageParamsWithType)
}

/**
 * 显示自定义消息
 * @param config 消息配置
 */
export const showMessage = (config: MessageConfig) => {
  ElMessage(config as unknown as MessageParamsWithType)
}

/**
 * 显示操作成功消息
 * @param operation 操作名称
 */
export const showOperationSuccess = (operation: string) => {
  showSuccess(`${operation}成功`)
}

/**
 * 显示操作失败消息
 * @param operation 操作名称
 * @param error 错误信息
 */
export const showOperationError = (operation: string, error?: string) => {
  showError(`${operation}失败${error ? `: ${error}` : ''}`)
}

/**
 * 显示权限不足消息
 */
export const showPermissionDenied = () => {
  showWarning('权限不足，请联系管理员')
}

/**
 * 显示网络错误消息
 */
export const showNetworkError = () => {
  showError('网络连接失败，请检查网络设置')
}

/**
 * 显示服务器错误消息
 */
export const showServerError = () => {
  showError('服务器内部错误，请稍后重试')
}

/**
 * 显示数据加载失败消息
 */
export const showLoadError = (dataName: string) => {
  showError(`加载${dataName}失败`)
}

/**
 * 显示数据保存失败消息
 */
export const showSaveError = (dataName: string) => {
  showError(`保存${dataName}失败`)
}

/**
 * 显示数据删除失败消息
 */
export const showDeleteError = (dataName: string) => {
  showError(`删除${dataName}失败`)
}

/**
 * 显示功能待实现消息
 */
export const showFeatureNotImplemented = (featureName: string) => {
  showInfo(`${featureName}功能待实现`)
}

/**
 * ElTag类型映射函数
 * 确保type属性类型安全
 * Element Plus中ElTag的type属性不支持'error'，使用'danger'替代
 */
export const getTagType = (type: 'success' | 'warning' | 'info' | 'error' | 'danger'): 'success' | 'warning' | 'info' | 'danger' => {
  // 将'error'映射为'danger'，因为Element Plus的ElTag不支持'error'类型
  return type === 'error' ? 'danger' : type
}

/**
 * 状态标签类型映射
 */
export const getStatusTagType = (status: number): 'success' | 'warning' | 'info' | 'danger' => {
  switch (status) {
    case 1: return 'success'
    case 0: return 'danger'
    case 2: return 'warning'
    default: return 'info'
  }
}

/**
 * 优先级标签类型映射
 */
export const getPriorityTagType = (priority: number): 'success' | 'warning' | 'info' | 'danger' => {
  switch (priority) {
    case 1: return 'info'
    case 2: return 'warning'
    case 3: return 'danger'
    default: return 'info'
  }
}

/**
 * 用户类型标签映射
 */
export const getUserTypeTagType = (userType: number): 'success' | 'warning' | 'info' | 'danger' => {
  switch (userType) {
    case 1: return 'success'
    case 2: return 'warning'
    case 3: return 'info'
    default: return 'info'
  }
}

/**
 * 操作类型标签映射
 */
export const getOperationTagType = (operation: string): 'success' | 'warning' | 'info' | 'danger' => {
  switch (operation) {
    case 'CREATE': return 'success'
    case 'UPDATE': return 'warning'
    case 'DELETE': return 'danger'
    case 'QUERY': return 'info'
    default: return 'info'
  }
} 